﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BUS;
using System.Text;
using System.Net;
using System.IO;

public partial class Admin_QuanLySanPham : System.Web.UI.Page
{
    DataTable dtSanPham;
    DataTable dtTenLoaiSP;
    DataTable dtSP;
    static String masp;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
            Response.Redirect("../Default.aspx");
        if (Boolean.Parse(Session["QuanLy"].ToString()) == false)
            Response.Redirect("../Default.aspx");
        if (!IsPostBack)
        {
            bindgrid();
            cmbLoaiSP.DataSource = dtTenLoaiSP = QuanLySanPhamBUS.LayTenLoaiSP();
            cmbLoaiSP.DataTextField = "TenLoaiSP";
            cmbLoaiSP.DataBind();
        }
    }
    protected void bindgrid()
    {
        dtSanPham = QuanLySanPhamBUS.LayThongTinSanPham();
        for (int i = 0; i < dtSanPham.Rows.Count; i++)
        {
            if (String.IsNullOrEmpty(dtSanPham.Rows[i][5].ToString()))
            {
                dtSanPham.Rows[i][5] = "0";
            }
            if (String.IsNullOrEmpty(dtSanPham.Rows[i][6].ToString()))
            {
                dtSanPham.Rows[i][6] = "0";
            }
        }
        gv_SanPham.DataSource = dtSanPham; ;
        gv_SanPham.DataBind();
    }
    protected float SoLuongCon(float slnhap, float slxuat)
    {
        return (slnhap - slxuat);
    }

    protected void gv_SanPham_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Int32 idx = Int32.Parse(e.CommandArgument.ToString());   
        if (e.CommandName.Equals("Sua"))
        {
            masp = gv_SanPham.Rows[idx].Cells[0].Text;
            dtSP = QuanLySanPhamBUS.LaySanPham(masp);
            tblThem.Visible = true;
            txtTenSP.Text = dtSP.Rows[0]["TenSP"].ToString();
            txtMoTaSP.Text = dtSP.Rows[0]["MoTaChiTiet"].ToString();
            txtDonViTinh.Text = dtSP.Rows[0]["DonViTinh"].ToString();
            cmbLoaiSP.Text = dtSP.Rows[0]["TenLoaiSP"].ToString();
            btnCapNhat.Text = "Cập nhật";
            btnThemSP.Visible = false;
            bindgrid();
            gotoAnchorScript("form");
        }
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        if (btnCapNhat.Text == "Cập nhật")
        {
            SanPham sp = new SanPham();
            sp.tensanpham = txtTenSP.Text;
            sp.motachitiet = txtMoTaSP.Text;
            sp.donvitinh = txtDonViTinh.Text;
            sp.maloaisp = QuanLySanPhamBUS.LayMaLoaiSP(cmbLoaiSP.Text);
            QuanLySanPhamBUS.CapNhatSanPham(sp, masp);
        }
        else
        {
            SanPham sp = new SanPham();
            sp.tensanpham = txtTenSP.Text;
            sp.motachitiet = txtMoTaSP.Text;
            sp.donvitinh = txtDonViTinh.Text;
            sp.maloaisp = QuanLySanPhamBUS.LayMaLoaiSP(cmbLoaiSP.Text);
            QuanLySanPhamBUS.ThemSP(sp);
        }
        Response.Redirect("QuanLySanPham.aspx#list");
    }
    private void gotoAnchorScript(string anchor)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#" + anchor + "';", true);
    }
    protected void btnThemSP_Click(object sender, EventArgs e)
    {
        tblThem.Visible = true;
        txtTenSP.Text = "";
        txtMoTaSP.Text = "";
        txtDonViTinh.Text = "";
        btnCapNhat.Text = "Lưu sản phẩm";
        btnThemSP.Visible = false;

        gotoAnchorScript("form");
    }
    protected void gv_SanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv_SanPham.PageIndex = e.NewPageIndex;
        bindgrid();
        gotoAnchorScript("list");
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuanLySanPham.aspx#list");
    }
    protected void gv_SanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        masp = gv_SanPham.Rows[e.RowIndex].Cells[0].Text;
        QuanLySanPhamBUS.XoaSanPham(masp);
        bindgrid();
    }
}
